package com.registration.service;

import com.registration.bean.DepartmentBean;
import com.registration.bean.DoctorBean;
import com.registration.mapper.DepartmentMapper;
import com.registration.mapper.DoctorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
    public class DepartmentServiceImpl implements DepartmentService {

        @Autowired
        private DepartmentMapper departmentMapper;

        // 注入医生相关的Mapper（因科室与医生为一对多关系，需查询医生表）
        @Autowired
        private DoctorMapper doctorMapper;


        @Override
        public List<DepartmentBean> getFirstLevelDepts() {
            return departmentMapper.selectFirstLevelDepts();
        }

        @Override
        public List<DepartmentBean> getSecondLevelDepts(Integer parentDeptId) {
            if (parentDeptId == null) {
                return Collections.emptyList();
            }
            return departmentMapper.selectSecondLevelDepts(parentDeptId);
        }

        @Override
        public List<DoctorBean> getDoctorsByDeptId(Integer deptId) {
            // 参数校验：科室ID为空时返回空列表
            if (deptId == null) {
                return Collections.emptyList();
            }
            // 调用DoctorMapper查询指定科室的医生（复用已定义的查询方法）
            // 对应设计文档中“科室与医生为一对多关系”的表结构设计
            return doctorMapper.selectDoctorsByDeptId(deptId);
        }

        @Override
        public DepartmentBean getDeptById(Integer deptId) {
            // 参数校验：科室ID为空时返回null
            if (deptId == null) {
                return null;
            }
            // 调用Mapper查询单个科室
            return departmentMapper.selectDeptById(deptId);
        }

        @Override
        public List<DepartmentBean> getAllDepartmentsWithHierarchy() {
            // 1. 一次性查询所有正常科室
            List<DepartmentBean> allDepartments = departmentMapper.selectAllDepartments();
            if (allDepartments == null || allDepartments.isEmpty()) {
                return Collections.emptyList();
            }

            // 2. 分离一级科室和二级科室，并按父ID分组
            List<DepartmentBean> firstLevelDepts = new ArrayList<>();
            Map<Integer, List<DepartmentBean>> secondLevelMap = new HashMap<>();

            for (DepartmentBean dept : allDepartments) {
                if (dept.getParent_dept_id() == null) {
                    // 一级科室：直接加入一级列表，初始化children集合
                    dept.setChildren(new ArrayList<>());
                    firstLevelDepts.add(dept);
                } else {
                    // 二级科室：按父ID存入Map分组
                    Integer parentId = dept.getParent_dept_id();
                    // 若Map中无该父ID的列表，则初始化
                    secondLevelMap.computeIfAbsent(parentId, k -> new ArrayList<>())
                            .add(dept);
                }
            }

            // 3. 为一级科室绑定对应的二级科室
            for (DepartmentBean firstLevel : firstLevelDepts) {
                Integer firstLevelId = firstLevel.getDept_id();
                // 从Map中获取当前一级科室的所有二级科室
                List<DepartmentBean> children = secondLevelMap.get(firstLevelId);
                if (children != null && !children.isEmpty()) {
                    firstLevel.getChildren().addAll(children);
                }
            }

            return firstLevelDepts;
        }
}
